document.addEventListener('DOMContentLoaded', () => {
  chrome.storage.sync.get(['lastSelectedDate'], (result) => {
    if (result.lastSelectedDate) {
      document.getElementById('datePicker').value = result.lastSelectedDate;
    }
  });
});

document.getElementById('calculateButton').addEventListener('click', () => {
  const selectedDate = document.getElementById('datePicker').value;
  if (selectedDate) {
    chrome.runtime.sendMessage({ action: 'calculateWorkdays', date: selectedDate }, (response) => {
      if (response.error) {
        document.getElementById('result').textContent = `Error: ${response.error}`;
      } else {
        document.getElementById('result').textContent = `Workdays: ${response.workdays}, Workhours: ${response.workhours} (The result includes the start date.)`;
      }
    });

    // Save the selected date to storage
    chrome.storage.sync.set({ lastSelectedDate: selectedDate }, () => {
      console.log('Date saved:', selectedDate);
    });
  } else {
    alert('Please select a date.');
  }
});